package com.sina.mysql.utils;

import java.math.BigInteger;
import java.time.LocalDate;

import static com.sina.mysql.utils.Assert.assertGeZero;
import static com.sina.mysql.utils.TimeUtils.CLOSE_DATE_FORMATTER;

/**
 * @ClassName BigIntegerSequence
 * @Description:
 * @Author 段浩杰
 * @Date 2018/8/22 17:40
 * @Version 1.0
 */
public class BigIntegerSequence {

    private final Snowflake snowflake;

    public BigIntegerSequence(Snowflake snowflake){
        this.snowflake = snowflake;
    }

    /**
     *
     * @param suffixNumber 大于等于 0
     */
    public BigInteger next(long suffixNumber){
        return new BigInteger( nextAsString( suffixNumber ) );
    }

    /**
     *
     * @param suffixNumber 大于等于 0
     */
    public  String nextAsString(long suffixNumber){
        assertGeZero(suffixNumber,"后缀数字不能是负数");

        String prefix =  LocalDate.now().format( CLOSE_DATE_FORMATTER );
        String sequence = String.valueOf( snowflake.next() );
        return prefix + sequence + suffixWithZero(suffixNumber);
    }

    private   String suffixWithZero(long number) {

        String str = String.valueOf( number % 10_0000 );
        int count = 5 - str.length();
        char[] chars = new char[count];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = '0';
        }
        return new String( chars ) + str;
    }


}
